package com.watchmen.mini.common.config;

import com.watchmen.mini.common.utils.ErrorMsg;
import com.watchmen.mini.common.utils.Result;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalExceptionHandler {

    protected org.slf4j.Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理自定义的业务异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = ServiceException.class)
    @ResponseBody
    public Result bizExceptionHandler(HttpServletRequest req, ServiceException e){
        logger.error("发生业务异常！原因是：{}",e);
        return new Result(e.getCode(),e.getMessage());
    }



    /**
     * 处理其他异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public Result exceptionHandler(HttpServletRequest req,Exception e){
        logger.error("未知异常！原因是:",e);
        return new Result(ErrorMsg.SYSTEM_ERROR);
    }
}